package zhengyanze.com.aipoemmax.guide.activity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.bean.PoemBean;
import zhengyanze.com.aipoemmax.guide.utils.DialogManager;
import zhengyanze.com.aipoemmax.guide.utils.LogUtils;

import static zhengyanze.com.aipoemmax.guide.activity.SuiBiActicity.SUIBI_FLAG;

public class AddSuibiActivity extends AppCompatActivity {
    private static final String SAVE_DATA = "save.data";
    @BindView(R.id.title)
    EditText title;
    @BindView(R.id.anthor)
    EditText anthor;
    @BindView(R.id.content)
    EditText content;

    boolean isSave = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_suibi);
        ButterKnife.bind(this);
        if (savedInstanceState != null) {
            PoemBean poemBean = savedInstanceState.getParcelable(SAVE_DATA);
            title.setText(poemBean.getTitle());
            anthor.setText(poemBean.getAuthor());
            content.setText(poemBean.getContent());
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (isSave) {
            PoemBean poemBean = new PoemBean(title.toString(),anthor.toString(),content.toString(),"");
            outState.putParcelable(SAVE_DATA,poemBean);
        }
    }

    @OnClick(R.id.save)
    public void onViewClicked() {
        isSave = false;
        final String tit = title.getText().toString();
        final String aut = anthor.getText().toString();
        final String con = content.getText().toString();
        if ("".equals(tit)) {
            Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("".equals(aut)) {
            Toast.makeText(this, "作者不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("".equals(con)) {
            Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        DialogManager.showDialog(this,"请稍后...");

        AVObject product = new AVObject("SuiBi_Product");
        product.put("title", tit);
        product.put("author", aut);
        product.put("content", con);
        product.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    Toast.makeText(AddSuibiActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                    finish();
                    if (mCallback != null) {
                        mCallback.updateUI(new PoemBean(tit,aut,con,SUIBI_FLAG));
                    }
                } else {
                    Toast.makeText(AddSuibiActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                DialogManager.dismissDialog();
            }
        });
    }


    //回调接口设置
    private static SuiBiActicity.backAddSuccess mCallback;
    public static void setCallback(SuiBiActicity.backAddSuccess callback) {
        mCallback = callback;
    }
}
